﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MentalAlchemy.Molecules;

namespace MentalAlchemy.Compounds
{
	public partial class ParseTreeControl : UserControl
	{
		private ParseTree tree;

		#region - Public properties. -
		public ParseTree Tree 
		{
			get { return tree; }
			set
			{
				if (value == null) return;
				SetTree(value);
			} 
		}
		#endregion

		#region - Constructor. -
		public ParseTreeControl()
		{
			InitializeComponent();
		} 
		#endregion

		#region - Public methods. -
		public void SetTree (ParseTree argTree)
		{
			tree = argTree;

			// show tree.
			treeView1.Nodes.Clear();
			var root = new TreeNode(tree.Name);
			SetChildNodes(root, tree.Nodes);
			treeView1.Nodes.Add(root);
		}
		#endregion

		#region - Private methods. -
		private static void SetChildNodes (TreeNode parent, List<ParseTreeNode> nodes)
		{
			foreach (var node in nodes)
			{
				var tempNode = new TreeNode(node.Contents);
				if (node.ChildNodes != null)
				{
					SetChildNodes(tempNode, node.ChildNodes);
				}
				parent.Nodes.Add(tempNode);
			}
		}
		#endregion
	}
}
